package com.example.mydianping.utils;

import com.example.mydianping.dto.Result;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {

    private boolean isBeenTexting = false;//是否跳过校验登录状态

    //前置拦截
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(isBeenTexting)return true;//如果在测试中则直接放行，不检验登录
        //判断线程池中是否有登录用户,若没有则拦截，返回 401 状态码
        if(UserHolder.getUser() == null){
//            System.out.println(request.getContextPath()+"请求被拦截");
            response.setStatus(401);
            return false;
        }
        return true;
    }

}
